package top.forforever.srb.core.listnener.excelListnener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import org.springframework.beans.BeanUtils;
import top.forforever.srb.core.mapper.DictMapper;
import top.forforever.srb.core.pojo.dto.ExcelDictDTO;
import top.forforever.srb.core.pojo.entity.Dict;

import java.util.ArrayList;
import java.util.List;

/**
 * @create: 2023/5/2
 * @Description:
 * @FileName: ExcelDictListnener
 * @自定义内容：
 */
public class ExcelDictListener extends AnalysisEventListener<ExcelDictDTO> {

    List<ExcelDictDTO> excelDictDTOS = new ArrayList<>();

    DictMapper dictMapper;

    public ExcelDictListener(DictMapper dictMapper) {
        this.dictMapper = dictMapper;
    }

    @Override
    public void invoke(ExcelDictDTO excelDictDTO, AnalysisContext context) {
        //批量添加字典
        excelDictDTOS.add(excelDictDTO);
        if (excelDictDTOS.size() % 10 == 0) {
            dictMapper.insertExcelDictDTO(excelDictDTOS);
            excelDictDTOS.clear();
        }
    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        //处理剩余的字典集合 收尾
        dictMapper.insertExcelDictDTO(excelDictDTOS);
    }
}
